var nameArr = [];
var valueArr = [];
var sum;

$(function () {
    ordersWeek();
})


function ordersWeek() {
    sum = 0;
    $.ajax({
        contentType: "application/json",
        url: "/ordersWeek",
        method: "post",
        data: " ",
        dataType: "json",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            nameArr = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
            // var  obj=JSON.parse(data);
            for (var i = 0; i < data.length; i++) {
                valueArr.push(data[i])
                sum += data[i] * 1;
            }
            sum = '本周订单总数:' + sum
            createOrderEchars();
            createOrderCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })


}

function ordersMonth() {
    sum = 0;
    $.ajax({
        contentType: "application/json",
        url: "/ordersMonth",
        method: "post",
        data: " ",
        dataType: "json",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            nameArr = ["第一周", "第二周", "第三周", "第四周", "第五周"];
            // var  obj=JSON.parse(data);
            for (var i = 0; i < data.length; i++) {
                valueArr.push(data[i])
                sum += data[i] * 1;
            }
            sum = '本月订单总数:' + sum
            createOrderEchars();
            createOrderCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })
}



function ordersBanYear() {
    // alert("年的方法")
    // debugger;
    sum = 0;
    $.ajax({
        contentType: "application/json",
        url: "/ordersYears",
        method: "post",
        data: " ",
        dataType: "text",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            var obj = data.split("&&");

            var name = JSON.parse(obj[0]);
            for (var i = name.length - 1; i >= 0; i--) {

                nameArr.push(name[i]);
            }
            var count = JSON.parse(obj[1])
            for (var i = 0; i < count.length; i++) {
                valueArr.push(count[i])
                sum += count[i] * 1;
            }
            sum = '近半年订单总数:' + sum

            createOrderEchars();
            createOrderCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })
}


function createOrderCurve() {

    var chartDom = document.getElementById('main2');
    var myChart = echarts.init(chartDom);
    var option;


    option = {
        xAxis: {
            type: 'category',
            data: nameArr,
        },
        yAxis: {
            type: 'value'
        },

        series: [{
            data: valueArr,
            type: 'line',
            smooth: true
        }]
    };

    option && myChart.setOption(option);

}


function createOrderEchars() {

// 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
    var option = {
        title: {
            text: sum,
            x: '600'
            // offsetCenter:[0,50]
        },
        tooltip: {},
        legend: {
            data: ['订单量']
        },
        xAxis: {
            data: nameArr
        },
        yAxis: {},
        series: [{
            name: '订单量',
            type: 'bar',
            barWidth: 55,
            data: valueArr
        }]
    };

// 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}


